﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using RATSharpW7.ViewModel;
using RATSharp.Model;
using System.Windows.Navigation;
using System.Windows.Data;
using System.ComponentModel;
using Microsoft.Phone.Shell;
using RATSharpW7.ViewModel.CalendarViewModel;

namespace RATSharpW7.View
{

    /// <summary>
    /// Lightweight object screen - it's the same
    /// like normal object profile, but this one does not provide
    /// any kind of action (e.g. object calendar)
    /// </summary>
    public partial class SimpleObjectProfile : PhoneApplicationPage
    {
        bool isNewPageInstance = false;
        ObjectProfileViewModel model;

        public SimpleObjectProfile()
        {
            InitializeComponent();

            isNewPageInstance = true;
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (e.NavigationMode !=
                  System.Windows.Navigation.NavigationMode.Back)
            {
                State["ObjectProfileViewModel"] = model;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (isNewPageInstance)
            {
                if (model == null)
                {
                    if (State.ContainsKey("ObjectProfileViewModel"))
                    {
                        MessageBox.Show("Retrieving state...");
                        model = State["ObjectProfileViewModel"]
                                           as ObjectProfileViewModel;
                    }
                    else
                    {
                        var data = NavigationContext.GetData<RATSharp.Model.Object>();

                        model = new ObjectProfileViewModel(data);
                    }
                }
                DataContext = model;
            }
            isNewPageInstance = false;
        }

    }
}